package main

import (
	"fmt"
	"net"
)

func main() {
	fmt.Println("tcp服务器端启动")
	listen, err := net.Listen("tcp", "localhost:8888")
	if err != nil {
		fmt.Println("创建监听失败", err)
		return
	}
	for {
		conn, err := listen.Accept()
		if err != nil {
			fmt.Println("获取连接失败", err)
			return
		}
		fmt.Println("连接成功", conn.RemoteAddr())
		go pross(conn)
	}
}

func pross(conn net.Conn) {
	defer func(conn net.Conn) {
		err := conn.Close()
		if err != nil {
			fmt.Println("关闭失败")
		}
	}(conn)

	for {
		paramIn := make([]byte, 1024)
		n, err := conn.Read(paramIn)
		if err != nil {
			return
		}
		fmt.Printf("收到%d个字节，%d, %v\n", n, len(paramIn), string(paramIn))
	}

}
